﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using BlueCMS.Core.Data;
using BlueCMS.Core.Data.Query.Clauses;
using BlueCMS.Core.Data.Query.Columns;
using BlueCMS.Core.Data.Query.Conditions;
using BlueCMS.Core.Data.Query.Tables;
using BlueCMS.Core.Providers;

namespace BlueCMS.Core.Data.Query.Generator
{
    public abstract partial class QueryGeneratorBase
    {
        protected virtual void GenerateOrderByStatement(OrderByClause clause, StringBuilder sb)
        {
            if (clause.OrderByColumns.Count == 0)
            {
                return;
            }

            sb.Append(" ORDER BY ");

            foreach (OrderByColumn column in clause.OrderByColumns)
            {
                switch (column.Type)
                {
                    case OrderByType.Alias:
                            GenerateAliasName((string)column.NameOrExpression, sb);
                        break;
                    case OrderByType.Column:
                            sb.Append(column.NameOrExpression);
                        break;
                    case OrderByType.Index:
                            sb.Append(column.NameOrExpression);
                        break;
                }
                if (column.SortDirection == SortDirection.Descending)
                {
                    sb.Append(" DESC");
                }
                sb.Append(", ");
            }
            sb.Remove(sb.Length - 2, 2);
        }
    }
}
